package com.medical.goods.service.impl;

import com.medical.goods.dao.CommodityDao;
import com.medical.goods.entity.CommodityEntity;
import com.medical.goods.entity.vo.SeckillVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.medical.common.utils.PageUtils;
import com.medical.common.utils.Query;

import com.medical.goods.dao.SeckillDao;
import com.medical.goods.entity.SeckillEntity;
import com.medical.goods.service.SeckillService;


@Service("seckillService")
public class SeckillServiceImpl extends ServiceImpl<SeckillDao, SeckillEntity> implements SeckillService {

    @Autowired
    private SeckillDao seckillDao;

    @Autowired
    private CommodityDao commodityDao;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SeckillEntity> page = this.page(
                new Query<SeckillEntity>().getPage(params),
                new QueryWrapper<SeckillEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void selectByIds(Integer[] ids) {
        List<CommodityEntity> commodityEntities = commodityDao.selectBatchIds(Arrays.asList(ids));
        for (CommodityEntity commodityEntity : commodityEntities) {
            SeckillEntity seckillEntity = new SeckillEntity();
            seckillEntity.setGid(commodityEntity.getId());
            seckillEntity.setCoding(commodityEntity.getCoding());
            seckillEntity.setImages(commodityEntity.getImages());
            seckillEntity.setPrice(commodityEntity.getPrice());
            seckillEntity.setName(commodityEntity.getName());
            seckillDao.insert(seckillEntity);
        }

    }

    @Override
    public void amend(List<SeckillVo> seckillVos) {
        for (SeckillVo seckillVo : seckillVos) {
            SeckillEntity seckillEntity = seckillDao.selectById(seckillVo.getId());
            seckillEntity.setSeckillPrice(seckillVo.getSeckillPrice());
            seckillEntity.setState(seckillVo.getState());
            seckillEntity.setEnddate(seckillVo.getEnddate());
            seckillEntity.setId(seckillVo.getId());
            seckillEntity.setStartdate(seckillVo.getStartdate());
            seckillDao.updateById(seckillEntity);

        }
    }


}